package com.jf.dao.impl;

import com.jf.bean.Teacher;
import com.jf.dao.TeacherDao;
import com.jf.util.BaseDao;
import com.jf.util.MyUtils;
import com.jf.util.PageInfo;

import java.util.ArrayList;
import java.util.List;

public class TeacherDaoImpl implements TeacherDao {

    @Override
    public List<Teacher> getPage(PageInfo pageInfo, Teacher teacher) {
        List<Object> objs = new ArrayList<>();
        String sql = "select * from teacher where 1 = 1 ";
        if(MyUtils.isStringNotEmpty(teacher.getName())){
            sql+="and name like concat(\"%\",?,'%') ";
            objs.add(teacher.getName());
        }
        if(teacher.getStatus() != null){
            sql+="and status = ? ";
            objs.add(teacher.getStatus());
        }
        sql+="order by id desc limit ?,?";
        objs.add(pageInfo.getOffset());
        objs.add(pageInfo.getPageSize());
        return BaseDao.baseQuery(Teacher.class,sql,objs);
    }

    @Override
    public int getCount(Teacher teacher) {
        List<Object> objs = new ArrayList<>();
        String sql = "select count(id) from teacher where 1 = 1 ";
        if(MyUtils.isStringNotEmpty(teacher.getName())){
            sql+="and name like concat(\"%\",?,'%') ";
            objs.add(teacher.getName());
        }
        if(teacher.getStatus() != null){
            sql+="and status = ? ";
            objs.add(teacher.getStatus());
        }
        return BaseDao.baseQueryCount(sql,objs);
    }

    @Override
    public int addSelect(Teacher teacher) {
        List<Object> objs = new ArrayList<>();
        String sql = "insert into teacher(";
        if(teacher.getId()!= null){
            sql+="id,";
        }
        if(MyUtils.isStringNotEmpty(teacher.getName())){
            sql+="name,";
        }
        if(MyUtils.isStringNotEmpty(teacher.getSrc())){
            sql+="src,";
        }
        if(MyUtils.isStringNotEmpty(teacher.getJob())){
            sql+="job,";
        }
        if(MyUtils.isStringNotEmpty(teacher.getIntroduction())){
            sql+="introduction,";
        }
        if(teacher.getStatus() != null){
            sql+="status,";
        }
        sql=sql.substring(0,sql.lastIndexOf(","));
        sql+=") values(";
        if(teacher.getId()!= null){
            sql+="?,";
            objs.add(teacher.getId());
        }
        if(MyUtils.isStringNotEmpty(teacher.getName())){
            sql+="?,";
            objs.add(teacher.getName());
        }
        if(MyUtils.isStringNotEmpty(teacher.getSrc())){
            sql+="?,";
            objs.add(teacher.getSrc());
        }
        if(MyUtils.isStringNotEmpty(teacher.getJob())){
            sql+="?,";
            objs.add(teacher.getJob());
        }
        if(MyUtils.isStringNotEmpty(teacher.getIntroduction())){
            sql+="?,";
            objs.add(teacher.getIntroduction());
        }
        if(teacher.getStatus() != null){
            sql+="?,";
            objs.add(teacher.getStatus());
        }
        sql=sql.substring(0,sql.lastIndexOf(","));
        sql+=")";
        return BaseDao.baseUpdate(sql,objs);
    }

    @Override
    public int updateSelect(Teacher teacher) {
        List<Object> objs = new ArrayList<>();
        String sql = "update teacher set ";
        if(MyUtils.isStringNotEmpty(teacher.getName())){
            sql+="name = ?,";
            objs.add(teacher.getName());
        }
        if(MyUtils.isStringNotEmpty(teacher.getSrc())){
            sql+="src = ?,";
            objs.add(teacher.getSrc());
        }
        if(MyUtils.isStringNotEmpty(teacher.getJob())){
            sql+="job = ?,";
            objs.add(teacher.getJob());
        }
        if(MyUtils.isStringNotEmpty(teacher.getIntroduction())){
            sql+="introduction = ?,";
            objs.add(teacher.getIntroduction());
        }
        if(teacher.getStatus() != null){
            sql+="status = ?,";
            objs.add(teacher.getStatus());
        }
        sql=sql.substring(0,sql.lastIndexOf(","));
        sql+=" where id = ?";
        objs.add(teacher.getId());
        return BaseDao.baseUpdate(sql,objs);
    }

    @Override
    public int batchDelete(int[] ids) {
        List<Object> objs = new ArrayList<>();
        StringBuilder sql = new StringBuilder("delete from teacher where id in(");
        for(int id : ids){
            sql.append("?,");
            objs.add(id);
        }
        sql=new StringBuilder(sql.substring(0,sql.lastIndexOf(",")));
        sql.append(")");
        return BaseDao.baseBatch(sql.toString(),ids.length,objs);
    }

    @Override
    public Teacher getById(int id) {
        String sql = "select * from teacher where id = ?";
        return BaseDao.baseQueryBean(Teacher.class,sql,id);
    }

    @Override
    public List<Teacher> getList() {
        String sql = "select * from teacher where status = 1";
        return BaseDao.baseQuery(Teacher.class,sql);
    }
}
